package nl.unimaas.micc.umsnooker.simulation.event;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import nl.unimaas.micc.umsnooker.adt.def.Position;
import nl.unimaas.micc.umsnooker.adt.def.PositionContainer;
import nl.unimaas.micc.umsnooker.adt.impl.KeyElementPair;
import nl.unimaas.micc.umsnooker.domain.BilliardBall;

public abstract class Event
{		
	public Event(float time, Set<BilliardBall> balls)
	{
		this.time = time;
		this.balls = balls;
		this.listOccurrences = new ArrayList<Position<Event>>(2);
	}

	public void addListOccurrence(Position<Event> position)
	{
		listOccurrences.add(position);
	}
	
	public void setPQOccurrence(Position<KeyElementPair<Float, Event>> position)
	{
		pqOccurrence = position;
	}

	public void removeOccurrences()
	{
		for (Position<Event> p : listOccurrences)
		{
			p.container().remove(p);
		}
		pqOccurrence.container().remove(pqOccurrence);
	}

	public float getTime()
	{
		return time;
	}
	
	public Set<BilliardBall> getBalls(Map<BilliardBall,BilliardBall> target)
	{
		Set<BilliardBall> balls = new HashSet<BilliardBall>();
		for(BilliardBall b : this.balls)
		{
			balls.add(target.get(b));
		}
		return balls;
	}
	
	public Set<BilliardBall> getBallKeys()
	{
		return this.balls;
	}
	
	public void setBalls(Set<BilliardBall> balls)
	{
		this.balls = balls;
	}

	@Override
	public String toString()
	{
		StringBuffer s = new StringBuffer("Event: " + this.getClass().getSimpleName() + "(t:"+getTime()+")");
		for(BilliardBall b : balls)
		{
			s.append(" :: " + b.getID());
		}
		return s.toString();
	}
	
	public abstract void handle(Map<BilliardBall,BilliardBall> target);

	private float time;
	private Set<BilliardBall> balls;
	private ArrayList<Position<Event>> listOccurrences;
	private Position<KeyElementPair<Float, Event>> pqOccurrence;

}
